package com.bosssoft.setp.handler;

import com.bosssoft.setp.model.business.aliance.AlianceBusiness;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.core.exception.EruptApiErrorTip;
import xyz.erupt.jpa.dao.EruptDao;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 该类是针对 AlianceBusiness 实体的数据代理类，用于在数据内容变更时进行验证。
 *
 * @author lin-gaoying
 * @version 1.0
 * @since 1.0
 */
@Service
public class AlianceBusinessProxy implements DataProxy<AlianceBusiness> {

    @Resource
    private EruptDao eruptDao;

    /**
     * 在添加AlianceBusiness数据前调用该方法进行手机号、邮箱、名称的格式验证。
     * 如果手机号或邮箱格式不正确，将抛出异常提示用户。
     *
     * @param alianceBusiness 待添加的AlianceBusiness实体对象
     */
    @Override
    public void beforeAdd(AlianceBusiness alianceBusiness) {

        String phoneNumber = alianceBusiness.getPhoneNumber();
        String mailbox = alianceBusiness.getMailbox();

        if (phoneNumber != null && !phoneNumber.isEmpty()) {
            boolean matches = Pattern.matches(
                    "^1[3-9]\\d{9}$|" +
                            "^1[3-9]\\d{1}[-\\s]\\d{4}[-\\s]\\d{4}$|" +
                            "^\\(1[3-9]\\d{1}\\)\\d{4}-\\d{4}$|" +
                            "^(?:\\(\\+\\d{2}\\)|\\+\\d{2})(\\d{11})$|" +
                            "^0\\d{3}-\\d{7}$|" +
                            "^0\\d{2}-\\d{8}$", phoneNumber);
            if (!matches) {
                throw new EruptApiErrorTip("请输入正确的手机号！");
            }
        }

        if (mailbox != null && !mailbox.isEmpty()) {
            boolean matches = Pattern.matches(
                    "^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+" +
                            "([-.][A-Za-z0-9]+)*\\.\\w+" +
                            "([-.][A-Za-z0-9]+)*$", mailbox);
            if (!matches) {
                throw new EruptApiErrorTip("请输入正确的邮箱！");
            }
        }

        List<AlianceBusiness> list = eruptDao.lambdaQuery(AlianceBusiness.class)
                .eq(AlianceBusiness::getName, alianceBusiness.getName())
                .list();
        if (!list.isEmpty()) {
            throw new EruptApiErrorTip("该加盟商已存在！");
        }

    }

    /**
     * 在修改AlianceBusiness数据前调用该方法进行手机号、邮箱、名称的格式验证。
     * 如果手机号或邮箱格式不正确，将抛出异常提示用户。
     *
     * @param alianceBusiness 待添加的AlianceBusiness实体对象
     */
    @Override
    public void beforeUpdate(AlianceBusiness alianceBusiness) {
        String phoneNumber = alianceBusiness.getPhoneNumber();
        String mailbox = alianceBusiness.getMailbox();
        Long id = alianceBusiness.getId();

        if (phoneNumber != null && !phoneNumber.isEmpty()) {
            boolean matches = Pattern.matches(
                    "^1[3-9]\\d{9}$|" +
                            "^1[3-9]\\d{1}[-\\s]\\d{4}[-\\s]\\d{4}$|" +
                            "^\\(1[3-9]\\d{1}\\)\\d{4}-\\d{4}$|" +
                            "^(?:\\(\\+\\d{2}\\)|\\+\\d{2})(\\d{11})$|" +
                            "^0\\d{3}-\\d{7}$|" +
                            "^0\\d{2}-\\d{8}$", phoneNumber);
            if (!matches) {
                throw new EruptApiErrorTip("请输入正确的手机号！");
            }
        }

        if (mailbox != null && !mailbox.isEmpty()) {
            boolean matches = Pattern.matches(
                    "^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+" +
                            "([-.][A-Za-z0-9]+)*\\.\\w+" +
                            "([-.][A-Za-z0-9]+)*$", mailbox);
            if (!matches) {
                throw new EruptApiErrorTip("请输入正确的邮箱！");
            }
        }

        List<AlianceBusiness> list = eruptDao.lambdaQuery(AlianceBusiness.class)
                .eq(AlianceBusiness::getName, alianceBusiness.getName())
                .ne(AlianceBusiness::getId, id)
                .list();
        if (!list.isEmpty()) {
            throw new EruptApiErrorTip("该加盟商名称已存在！");
        }
    }

    /**
     * 在修改AlianceBusiness数据前调用该方法进行手机号、邮箱、名称的格式验证。
     * 如果手机号或邮箱格式不正确，将抛出异常提示用户。
     *
     * @param alianceBusiness 待添加的AlianceBusiness实体对象
     */
    @Override
    public void beforeDelete(AlianceBusiness alianceBusiness){
        String sql = "SELECT COUNT(*) FROM setp_training_place WHERE aliance_business_id = ?";
        Long count = eruptDao.getJdbcTemplate().queryForObject(
                sql, Long.class, alianceBusiness.getId());
        if (count > 0) {
            throw new EruptApiErrorTip("删除失败！当前加盟商还存在子门店。");
        }

        String sql1 = "SELECT COUNT(*) FROM setp_privilege_alliance_business_course WHERE alliance_id = ?";
        Long count1 = eruptDao.getJdbcTemplate().queryForObject(
                sql1, Long.class, alianceBusiness.getId());
        if (count1 > 0) {
            throw new EruptApiErrorTip("该教师有授权课程，无法删除,请解绑关联的课程");
        }

        String sql2 = "SELECT COUNT(*) FROM setp_privilege_alliance_business_member WHERE setp_mumber_link_alliance = ?";
        Long count2 = eruptDao.getJdbcTemplate().queryForObject(
                sql2, Long.class, alianceBusiness.getId());
        if (count2 > 0) {
            throw new EruptApiErrorTip("删除失败，该加盟商旗下存在人员，请解绑人员关系");
        }
    }

}
